package com.lt.contract.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lt.contract.bean.NM_CONTRACT_2019;
import com.lt.contract.bean.utils.ExcelImport;
import com.lt.contract.entity.InnovativeBusiness;
import com.lt.contract.entity.NmInternetOfThings;
import com.lt.contract.entity.NmMobileService;
import com.lt.contract.dao.NmMobileServiceDao;
import com.lt.contract.entity.NmMongoliaCircuitSharing;
import com.lt.contract.service.NmMobileServiceService;
import com.lt.report.service.AsyncService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 移动业务(NmMobileService)表服务实现类
 *
 * @author makejava
 * @since 2020-02-26 11:40:06
 */
@Slf4j
@Service("nmMobileServiceService")
public class NmMobileServiceServiceImpl implements NmMobileServiceService {
    @Resource
    private NmMobileServiceDao nmMobileServiceDao;

    @Autowired
    AsyncService asyncService;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public NmMobileService queryById(String id) {
        return this.nmMobileServiceDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<NmMobileService> queryAllByLimit(int offset, int limit) {
        return this.nmMobileServiceDao.queryAllByLimit(offset, limit);
    }

    @Override
    public List<NmMobileService> queryAll(Map<String, Object> map) throws Exception {
        return nmMobileServiceDao.queryAll(map);
    }

    @Override
    public Long queryCount(Map<String, Object> map) throws Exception {
        return nmMobileServiceDao.queryCount(map);
    }

    /**
     * 新增数据
     *
     * @param nmMobileService 实例对象
     * @return 实例对象
     */
    @Override
    public NmMobileService insert(NmMobileService nmMobileService) {
        this.nmMobileServiceDao.insert(nmMobileService);
        return nmMobileService;
    }

    /**
     * 修改数据
     *
     * @param nmMobileService 实例对象
     * @return 实例对象
     */
    @Override
    public NmMobileService update(NmMobileService nmMobileService) {
        this.nmMobileServiceDao.update(nmMobileService);
        return this.queryById(nmMobileService.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String id) {
        return this.nmMobileServiceDao.deleteById(id) > 0;
    }



    @Override
    public String getBankListByExcel(InputStream in, String fileName,String staffId) throws Exception {
        //选择文件中的列
        String tableName = "";
        if(fileName.contains("第二发展人")){
            tableName ="mobile";
        }else {
            log.error("上传文件不符合规定");
            return "请上次相关文件";
        }
        //创建Excel工作薄
        Workbook work = ExcelImport.getWorkbook(in, fileName);
        Sheet sheet = work.getSheetAt(0);
        List<Integer> column = Arrays.asList(3,4,5,6,7,10,11);
        String empty = ExcelImport.isEmpty(sheet, column);
        if(!empty.equals("true")){
            return empty;
        }
        List<String> isRecur = ExcelImport.isRecur(sheet,10);
        if(isRecur.size()>0){
            JSONObject object = new JSONObject();
            object.put("您上传的业务号码有重复，请确认！",isRecur);
            return JSON.toJSONString(object);
        }
        List<List<String>> list = new ArrayList<>();
        list = ExcelImport.queryRepetition(sheet,10);//第10列是计费号码
        List<Map<String,Object>> list1 = new ArrayList<>();
        for (int i = 0; i <list.size() ; i++) {
            list1.addAll(nmMobileServiceDao.queryRepetition(list.get(i)));
        }
        if(list1.size()>0){
            return JSON.toJSONString(list1);
        }
        if(tableName.equals("mobile")) {
            List<List<NmMobileService>> lists =listNmMobile(sheet,staffId);
            int size = lists.size();
            for (int i = 0; i <size ; i++) {
                List<NmMobileService> listI = lists.get(i);
                asyncService.executeMobile(listI);
            }
        } else {
            return "请上次相关文件";
        }
        return "上传成功";
    }



    /**
     * 导入第二发展人
     *
     * @param sheets 第二发展人
     * @return
     */
    public List<List<NmMobileService>> listNmMobile(Sheet sheets,String staffId) throws Exception {
        List<List<NmMobileService>> lists =new ArrayList<>();
        List<NmMobileService> list = new ArrayList<>();
        Cell cell = null;
        for (int j = sheets.getFirstRowNum() + 1; j <= sheets.getLastRowNum()+1; j++) {
            Row row = sheets.getRow(j);
            int flag =200;
            if(list.size() !=0 && list.size()%flag ==0 || j == sheets.getLastRowNum()+1){
                List<NmMobileService> copy = new ArrayList<>(list);
                lists.add(copy);
                list.clear();
            }
            if (row == null || row.getFirstCellNum() == j) {
                continue;
            }else if (ExcelImport.isAllRowEmpty(row,row)){
                continue;
            }
            NmMobileService mobileService = new NmMobileService();
            for (int y = row.getFirstCellNum(); y < row.getLastCellNum(); y++) {
                cell = row.getCell(y);
                if(cell==null || cell.toString().equals("")){
                    continue;
                }
                switch (y+1){
                    case 2:mobileService.setAreaCode(ExcelImport.getCellValue(cell)); break;
                    case 3:mobileService.setReserved1(ExcelImport.getCellValue(cell)); break;//网别 5001固网 5002 移网
                    case 4:mobileService.setTrellisCode(ExcelImport.getCellValue(cell)); break;
                    case 5:mobileService.setTrellisName(ExcelImport.getCellValue(cell)); break;
                    case 6:mobileService.setYzStaffId(ExcelImport.getCellValue(cell)); break;
                    case 7:mobileService.setAccountManager(ExcelImport.getCellValue(cell)); break;
                    case 8:mobileService.setContractNoGlobal(ExcelImport.getCellValue(cell)); break;
                    case 9:mobileService.setContractName(ExcelImport.getCellValue(cell)); break;
                    case 10:mobileService.setRepositoryNameVendor(ExcelImport.getCellValue(cell)); break;
                    case 11:mobileService.setSerialNumber(ExcelImport.getCellValue(cell)); break;
                    case 12:mobileService.setCycleId(ExcelImport.getCellValue(cell)); break;
                    case 13:mobileService.setFee(ExcelImport.getCellValue(cell)); break;
                }

            }
            mobileService.setInputStaffId(staffId);
            mobileService.setUpdateStaffid(staffId);
            list.add(mobileService);
        }
        return lists;
    }

}
